package goodbaby.dklt.activity;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

import com.classic.core.utils.FileUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import goodbaby.dklt.R;

/**
 * Created by Jayden on 2016/5/4 11:31.
 *
 * @version V1.0
 * @Title: SurfaceActivity
 * @Description: 视频播放
 */
public class SurfaceActivity extends Activity implements SurfaceHolder.Callback {
    private Button btnBack;
    MediaPlayer player;
    SurfaceView surface;
    SurfaceHolder surfaceHolder;
    private int type = 0;
    private String vedioUrl;
    private Timer timer = new Timer();

    public void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_surface);
        type = getIntent().getIntExtra("type", 0);
        vedioUrl = getIntent().getStringExtra("vedioUrl");
        surface = (SurfaceView) findViewById(R.id.surface);
//        surface.setOnClickListener(new View.OnClickListener() {
//            public void onClick(View paramAnonymousView) {
//                player.start();
//            }
//        });
        btnBack = (Button) findViewById(R.id.atlas_see_title_back);
        btnBack.setOnClickListener(new View.OnClickListener() {
            public void onClick(View paramAnonymousView) {
//                if (player.isPlaying())
//                    player.stop();
//                player.release();
                finish();
            }
        });
        surfaceHolder = surface.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setFixedSize(320, 220);
        surfaceHolder.setType(3);
    }

    protected void onDestroy() {
        super.onDestroy();
        if (player.isPlaying())
            player.stop();
        player.release();
    }


    public void surfaceChanged(SurfaceHolder paramSurfaceHolder, int paramInt1, int paramInt2, int paramInt3) {
    }

    public void surfaceCreated(SurfaceHolder paramSurfaceHolder) {
        final Context context = this;
        player = new MediaPlayer();
        player.setAudioStreamType(3);
        player.setDisplay(surfaceHolder);
        try {
            if (type == 0) {
                player.setDataSource(vedioUrl);
                player.prepare();
                player.start();
            } else {
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        Uri localUri = Uri.parse(vedioUrl);
                        try {
                            player.setDataSource(context, localUri);
                            player.prepare();
                            player.start();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }, 500);
            }

        } catch (Exception localException) {
            localException.printStackTrace();
        }
    }

    public void surfaceDestroyed(SurfaceHolder paramSurfaceHolder) {
    }
}
